package com.zheng.api.server.service.task;

import com.zheng.api.common.constant.ApiBaseResult;
import com.zheng.api.common.constant.ApiResultConstant;
import com.zheng.api.server.service.WxTemplateService;
import com.zheng.business.common.constant.BusinessConstant;
import com.zheng.business.dao.model.BusinessOrderUmbrellaConsumption;
import com.zheng.business.dao.model.BusinessOrderUmbrellaConsumptionExample;
import com.zheng.business.rpc.api.BusinessOrderUmbrellaConsumptionService;
import com.zheng.business.rpc.api.BusinessOrderUmbrellaDepositService;
import com.zheng.common.util.DateUtil;
import com.zheng.umbrella.rpc.api.UmbrellaRuleSpecialconfigDetailService;
import com.zheng.upms.rpc.api.UpmsSystemParamService;
import com.zheng.wechat.mp.rpc.api.WechatMpSystemAccountService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 超时前系统自动提醒用户归还
 */
@Service
public class TimeoutOrderRemindTaskService {

    private static final Logger LOGGER = LoggerFactory.getLogger(TimeoutOrderRemindTaskService.class);

    @Autowired
    UpmsSystemParamService upmsSystemParamService;
    @Autowired
    WechatMpSystemAccountService wechatMpSystemAccountService;
    @Autowired
    BusinessOrderUmbrellaConsumptionService businessOrderUmbrellaConsumptionService;
    @Autowired
    BusinessOrderUmbrellaDepositService businessOrderUmbrellaDepositService;
    @Autowired
    UmbrellaRuleSpecialconfigDetailService umbrellaRuleSpecialconfigDetailService;
    @Autowired
    WxTemplateService wxTemplateService;

    public ApiBaseResult doBusinessOrderRemindTask(String bDates){
        LOGGER.debug("TimeoutOrderRemindTask begin：{}", DateUtil.getDate());
        try {
                //查询超时未归还的借伞订单
                BusinessOrderUmbrellaConsumptionExample orderExample = new BusinessOrderUmbrellaConsumptionExample();
                BusinessOrderUmbrellaConsumptionExample.Criteria orderCriteria = orderExample.createCriteria();
                //借伞成功
                orderCriteria.andStatusEqualTo(BusinessConstant.UM_CONSUMPTION_ORDER_STATUS_3);
                orderCriteria.andAutoendtimeIsNotNull();
                orderCriteria.andAgentidIsNotNull();
                int days = 3;
                Date endTime = DateUtil.getNowDate();
                try {
                    days = Integer.parseInt(bDates);
                    if (days <= 0) {
                        days = 3;
                    }
                }catch (Exception e){}
                Date beginDate = DateUtil.getBeginTime(DateUtil.adddays(endTime, days-1) );
                Date endDate = DateUtil.getBeginTime(DateUtil.adddays(endTime, days) );
                orderCriteria.andAutoendtimeGreaterThanOrEqualTo(beginDate);
                orderCriteria.andAutoendtimeLessThanOrEqualTo(endDate);
                List<BusinessOrderUmbrellaConsumption> orderList = businessOrderUmbrellaConsumptionService.selectByExample(orderExample);
                //逐笔发送提醒
                for (int i = 0; i < orderList.size(); i++) {
                    BusinessOrderUmbrellaConsumption order = orderList.get(i);
                    //发送微信小程序还伞提醒
                    wxTemplateService.miniAppTemplatePush(order.getOrderno(),3);
                }
        }catch (Exception e){
            LOGGER.error("TimeoutOrderRemindTask error",e);
        }
        LOGGER.debug("TimeoutOrderRemindTask end：{}", DateUtil.getDate());

        return new ApiBaseResult(ApiResultConstant.SUCCESS,null);
    }
}
